package com.gjh.controller.company;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.gjh.controller.BeanController;
import com.gjh.domain.company.Company;
import com.gjh.domain.company.PageBean;
import com.gjh.service.company.CompanyService;


import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/company")
public class CompanyController extends BeanController{

    @Reference
    private CompanyService companyService;

    @RequestMapping(value = "/list",name = "展示企业列表数据")
    public String findAll(@RequestParam(name ="page",defaultValue = "1")int pageNum,@RequestParam(name = "pageSize",defaultValue = "10")int pageSize){
//        List<Company> companyList = companyService.findAll();
//        request.setAttribute("list",companyList);
        PageInfo page= companyService.findByPage(pageNum,pageSize);
        request.setAttribute("page",page);
        return "company/company-list";
    }

    @RequestMapping(value = "/toAdd" ,name = "进入新增企业界面")
    public String toAdd(){
        return "company/company-add";
    }

    @RequestMapping(value = "/edit",name = "保存企业数据")
    public String edit(Company company){
        if(StringUtils.isEmpty(company.getId())){
            company.setId(UUID.randomUUID().toString());//设置企业id
            companyService.save(company);
        }else{
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入企业更新界面")
    public String toUpdate(String id){
        Company company=companyService.findById(id);
        request.setAttribute("company",company);
        return "company/company-update";
    }

    @RequestMapping(value = "/delete",name = "删除企业")
    public String deleteById(String id){
        companyService.deleteById(id);
        return "redirect:/company/list.do";
    }


}
